﻿using System;//
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.Threading;
using FormattingLib;

namespace Test.Formatting
{
    [TestClass]
    public class NumberFormattingTest1
    {


        //[TestMethod]
        //public void Formatting_throws_Test1()
        //{
        //    Exception ex = null;
        //    try
        //    {
        //        string formattedNumber = NumberFormatting.GetFormattedNumber(0, RoundingType.RoundToEven);
        //    }
        //    catch (Exception e)
        //    {
        //        ex = e;
        //    }
        //    Assert.IsNotNull(ex);
        //}


        [TestMethod]
        public void ToStringSample()
        {

            double d;
            string s;

            d = 1234;
            s = Convert.ToString(d);
            Assert.AreEqual("1234", s);

            d = 1234.1234;
            s = Convert.ToString(d);
            Assert.AreEqual("1234,1234", s);

            d = 1234.1234;
            d = Math.Round(d);
            s = Convert.ToString(d);
            Assert.AreEqual("1234", s);


            d = 1234.5;
            d = Math.Round(d);
            s = Convert.ToString(d);
            Assert.AreEqual("1234", s);

            d = 1234.6;
            d = Math.Round(d);
            s = Convert.ToString(d);
            Assert.AreEqual("1235", s);

            

        }


        [TestMethod]
        public void FormatStringSamples()
        {
            string formattedString;

            ////////////////////////////////
            // Digits after decimal point
            ////////////////////////////////

            // just two decimal places
            formattedString = String.Format("{0:0.00}", 123.4567);      // "123.46"
            formattedString = String.Format("{0:0.00}", 123.4);         // "123.40"
            formattedString = String.Format("{0:0.00}", 123.0);         // "123.00"

            // max. two decimal places
            formattedString = String.Format("{0:0.##}", 123.4567);      // "123.46"
            formattedString = String.Format("{0:0.##}", 123.4);         // "123.4"
            formattedString = String.Format("{0:0.##}", 123.0);         // "123"



            ////////////////////////////////
            // Digits before decimal point
            ////////////////////////////////

            // at least two digits before decimal point
            formattedString = String.Format("{0:00.0}", 123.4567);      // "123.5"
            formattedString = String.Format("{0:00.0}", 23.4567);       // "23.5"
            formattedString = String.Format("{0:00.0}", 3.4567);        // "03.5"
            formattedString = String.Format("{0:00.0}", -3.4567);       // "-03.5"



            ////////////////////////////////
            // Thousands separator
            ////////////////////////////////
            formattedString = String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
            formattedString = String.Format("{0:0,0}", 12345.67);       // "12,346"




            ////////////////////////////////
            // Zero
            ////////////////////////////////

            //Float numbers between zero and one can be formatted in two ways, 
            //with or without leading zero before decimal point. 
            //To format number without a leading zero use # before point. 
            //For example „#.0“ formats number to have one decimal place 
            //and zero to N digits before decimal point (e.g. „.5“ or „123.5“).
            formattedString = String.Format("{0:0.0}", 0.0);            // "0.0"
            formattedString = String.Format("{0:0.#}", 0.0);            // "0"
            formattedString = String.Format("{0:#.0}", 0.0);            // ".0"
            formattedString = String.Format("{0:#.#}", 0.0);            // ""




            ////////////////////////////////
            //Align numbers with spaces
            ////////////////////////////////
            formattedString = String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
            formattedString = String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
            formattedString = String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
            formattedString = String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

            


            ////////////////////////////////
            //Custom formatting for negative numbers and zero
            ////////////////////////////////
            formattedString = String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
            formattedString = String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
            formattedString = String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"




            ////////////////////////////////
            //Some funny examples
            ////////////////////////////////
            formattedString = String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
            formattedString = String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"



        }




        [TestMethod]
        public void FormatStringTest1()
        {
            var str1 = String.Format("{0:###,##0.000}", 2.0);
            Assert.AreEqual("2,000", str1);

            var str2 = String.Format("{0:###,##0.000}", 2.12);
            Assert.AreEqual("2,120", str2);

            var str3 = String.Format("{0:###,##0.000}", 2.12345);
            Assert.AreEqual("2,123", str3);

        }


        [TestMethod]
        public void FormatStringTest2()
        {
      
            string weightString1 = String.Format("{0:#.000}", 123.1234567);
            Assert.AreEqual("123,123", weightString1);
            
            string weightString3 = String.Format("{0:#}", 123.1234567);
            Assert.AreEqual("123", weightString3);


            string weightString4 = String.Format("{0:#.000}", 0.1234567);
            Assert.AreEqual(",123", weightString4);

            string weightString5 = String.Format("{0:0.000}", 0.1234567);
            Assert.AreEqual("0,123", weightString5);
            
        
        }



        [TestMethod]
        public void TestMethod2()
        {
            ///                        a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8
            /// =====================  ======  ======  =====  =====  =====
            /// Floor                    -3      -1      0      1      2
            /// Ceiling                  -2       0      1      2      3
            /// Truncate                 -2       0      0      1      2
            /// Round (ToEven)           -3       0      0      2      3
            /// Round (AwayFromZero)     -3      -1      0      2      3


            double rounded;
            string formatted;

            //Default-FormatierungsString : "{0:0.000}"
            //Default Decimals: 3
            //Default-Culture : "de-DE"

            rounded = NumberFormatting.GetRoundedNumber(2.8, RoundingType.Floor);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("2,000", formatted);

            rounded = NumberFormatting.GetRoundedNumber(2.5);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("2,500", formatted);

            rounded = NumberFormatting.GetRoundedNumber(2.55);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("2,550", formatted);

            rounded = NumberFormatting.GetRoundedNumber(2.5555);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("2,556", formatted);

            rounded = NumberFormatting.GetRoundedNumber(2.5, RoundingType.Round, 0);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("3,000", formatted);

            rounded = NumberFormatting.GetRoundedNumber(0.1234567);
            formatted = NumberFormatting.GetFormattedNumber(rounded);
            Assert.AreEqual("0,123", formatted);

            rounded = NumberFormatting.GetRoundedNumber(0.1234567);
            formatted = NumberFormatting.GetFormattedNumber(rounded, "0", new CultureInfo("en-US"), "{0:0.000}");
            Assert.AreEqual("0.123", formatted);
         
        }


        [TestMethod]
        public void TestMethod3()
        {
           string res1 =  Convert.ToString(Math.Round(1.4444));
           string res2 = Convert.ToString(Math.Round(1.5005));
           string res3 = Convert.ToString(Math.Round(1.5555));


           double rounded4 = NumberFormatting.GetRoundedNumber(1.4444, RoundingType.Round, 0);
           string res4 = NumberFormatting.GetFormattedNumber(rounded4, "1", null, "{0:#}");

           double rounded5 = NumberFormatting.GetRoundedNumber(1.5005, RoundingType.Round, 0);
           string res5 = NumberFormatting.GetFormattedNumber(rounded5, "1", null, "{0:#}");

           double rounded6 = NumberFormatting.GetRoundedNumber(1.5555, RoundingType.Round, 0);
           string res6 = NumberFormatting.GetFormattedNumber(rounded6, "1", null, "{0:#}");

           double rounded7 = NumberFormatting.GetRoundedNumber(0, RoundingType.Round, 0);
           string res7 = NumberFormatting.GetFormattedNumber(rounded7, "1", null, "{0:#}");


        }
    }
}

